package com.skt.common_hr.overtime.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.overtime.events.SelOrgEvent;
	import com.skt.common_hr.overtime.model.HolidayProxy;
	import com.skt.common_hr.overtime.model.MonthlyDeptListProxy;
	import com.skt.common_hr.overtime.model.OrgProxy;
	import com.skt.common_hr.overtime.model.vo.MonthlyDeptVO;
	import com.skt.common_hr.overtime.view.components.DeptOTView;
	import com.skt.common_hr.overtime.view.components.MemberOtView;
	import com.skt.common_hr.overtime.view.components.Overview;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class OvertimeMediator extends OverViewMediator
	{
		public static const NAME:String = "OvertimeMediator"; 

		public var bExecDeptOt:Boolean = false;
		public var bExecMemberOt:Boolean = false;
		public var bExecMemberListOt:Boolean = false;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function OvertimeMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }


		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function activatedView():void
		{
			setEventListeners();
			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			loadOrgData();
		}
		
		override protected function closedView():void
		{
			removeEventListeners();
		}
 
 		private function setEventListeners():void
 		{
 			view.addEventListener(Overview.CHANGED_MONTH, onChangedMonth);
 			view.addEventListener(Overview.EXEC_DEPT_OT, onExecDeptOT);
 			view.addEventListener(Overview.EXEC_MEMBER_OT, onExecMemberOT);
 			view.addEventListener(Overview.CLICK_ITEM, onClickItem);
 			view.addEventListener(Overview.CLICK_DEPT, onClickDept);
 			view.addEventListener(SelOrgEvent.SELECTED_ORG, onSelectedOrg);
 		}

 		private function removeEventListeners():void
 		{
 			view.removeEventListener(Overview.CHANGED_MONTH, onChangedMonth);
 			view.removeEventListener(Overview.EXEC_DEPT_OT, onExecDeptOT);
 			view.removeEventListener(Overview.EXEC_MEMBER_OT, onExecMemberOT);
 		}


 		private function loadOrgData():void
 		{
 			var orgProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
 			orgProxy.load(Variables.myPernr);
 		}
 		
 		private function loadData(month:String, selOrg:String):void
 		{
 			var proxy:MonthlyDeptListProxy = facade.retrieveProxy(MonthlyDeptListProxy.NAME) as MonthlyDeptListProxy;
			proxy.load(selOrg, month);
			
			var holiProxy:HolidayProxy = facade.retrieveProxy(HolidayProxy.NAME) as HolidayProxy;
			holiProxy.load(month);
 		}

		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private var selectedOrg:String = "";
		private function onSelectedOrg(e:SelOrgEvent):void
		{
			selectedOrg = e.orgCode;
			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			loadData(month, selectedOrg);
		}
		
		private function onChangedMonth(e:Event):void
		{
			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			loadData(month, selectedOrg);
		}

		private function onExecDeptOT(e:Event):void
		{
			execDeptOT();
		}
		
		private function onExecMemberOT(e:Event):void
		{
			if(view.selectedDate == null) {
				view.selectedDate = new Date();
			}
			if(view.selectedDept == null) {
				view.otList.getItemAt(0).ORGEH;
			}
			execMemberOT();
		}
		
		private function onClickItem(e:Event):void
		{
			execMemberOT();
		}
		
		private function onClickDept(e:Event):void
		{
			var month:String = view.selectedYear.toString();
			month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
			bExecMemberListOt = true;
			view.vsOt.selectedIndex = 1;
			
			var memberOtList:MemberOtListMediator= facade.retrieveMediator(MemberOtListMediator.NAME) as MemberOtListMediator;
			//memberOtList.exec(month, view.selectedDept, accessableDeptList());
			memberOtList.exec(month, view.getSelectedDeptVO(), accessableDeptList());
		}

		private function accessableDeptList():ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each(var rec:MonthlyDeptVO in view.otList) {
				var vo:Object = new Object();
				vo.label = rec.ORGTX;
				vo.data = rec.ORGEH;
				list.addItem(vo);
			}
			return list;
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_LIST_COMPLETE,
					MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_LIST_FAILED,
					DeptOtMediator.CLOSE_DEPT_OT,
					MemberOtMediator.CLOSE_MEMBER_OT_VIEW,
					MemberOtListMediator.CLOSE_MEMBER_OT_LIST_VIEW,
					OrgProxy.LOAD_ORGLIST_COMPLETE,
					OrgProxy.LOAD_ORGLIST_FAILED,
					HolidayProxy.LOAD_HOLIDAYS_COMPLETE
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_LIST_COMPLETE:
					view.otList = (note.getBody() as MonthlyDeptListProxy).list;
					view.orgTx = (note.getBody() as MonthlyDeptListProxy).ORGTX;
					
					//20110726
					view.acOrgeh = (note.getBody() as MonthlyDeptListProxy).listOrgeh;
					view.E_DOUBLAE = (note.getBody() as MonthlyDeptListProxy).e_doublae;
					
					//view.permit = (note.getBody() as MonthlyDeptListProxy).permit;
					view.bReadOnly = (note.getBody() as MonthlyDeptListProxy).bReadOnly;
					break;
				case MonthlyDeptListProxy.LOAD_MONTHLY_DEPT_LIST_FAILED:
					
					break;
				case DeptOtMediator.CLOSE_DEPT_OT:
					if(bExecDeptOt) {
						bExecDeptOt = false;
						var month:String = view.selectedYear.toString();
						month += (view.selectedMonth<10) ? "0"+view.selectedMonth.toString():view.selectedMonth.toString();
						loadData(month, selectedOrg);
					}
					break;
				case MemberOtMediator.CLOSE_MEMBER_OT_VIEW:
					if(bExecMemberOt) {
						bExecMemberOt = false;
					}
					break;
				case MemberOtListMediator.CLOSE_MEMBER_OT_LIST_VIEW:
					if(bExecMemberListOt) {
						bExecMemberListOt = false;
						view.vsOt.selectedIndex = 0;
					}
					break;
				case OrgProxy.LOAD_ORGLIST_COMPLETE:
					view.orgList = (note.getBody() as OrgProxy).orgList;
					view.permit = (note.getBody() as OrgProxy).permit;
					break;
				case OrgProxy.LOAD_ORGLIST_FAILED:
					CommonAlert.showMessage("", "조직데이터를 로드하는 중 오류가 발생했습니다.");
					break;
				case HolidayProxy.LOAD_HOLIDAYS_COMPLETE:
					view.holidays = new ArrayCollection((note.getBody() as HolidayProxy).list);
					view.setHoliday();
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
 		
 		
 		


		private var deptOtView:DeptOTView;
		private function execDeptOT():void
		{
			bExecDeptOt = true;
			var deptOtMediator:DeptOtMediator= facade.retrieveMediator(DeptOtMediator.NAME) as DeptOtMediator;
			if(deptOtMediator==null) {
	 			deptOtView = PopUpManager.createPopUp(view.root, DeptOTView, true) as DeptOTView;
	 			PopUpManager.centerPopUp(deptOtView);
	 			
				deptOtMediator = new DeptOtMediator(deptOtView);	
				facade.registerMediator(deptOtMediator);
			}	
			else {
				PopUpManager.addPopUp(deptOtMediator.view, view.root, true);
			}
			
			var d:Date = view.selectedDate;
			if(d==null) d = new Date();
			
			//var proxy:MonthlyDeptListProxy = facade.retrieveProxy(MonthlyDeptListProxy.NAME) as MonthlyDeptListProxy;
			//deptOtMediator.exec(d, proxy.ORGEH, proxy.ORGTX);
			deptOtMediator.exec(d, view.selectedDeptCode, view.selectedDeptName);
		}

		private var memberOtView:MemberOtView = null;
		private function execMemberOT():void
		{
			bExecMemberOt = true;
			var memberOtMediator:MemberOtMediator = facade.retrieveMediator(MemberOtMediator.NAME) as MemberOtMediator;
			if(memberOtMediator==null) {
				memberOtView = PopUpManager.createPopUp(view.root, MemberOtView, true) as MemberOtView;
				PopUpManager.centerPopUp(memberOtView);
				
				memberOtMediator = new MemberOtMediator(memberOtView);
				facade.registerMediator(memberOtMediator);
			}
			else {
				PopUpManager.addPopUp(memberOtMediator.view, view.root, true);
			}
			
			memberOtMediator.exec(view.selectedDate, view.selectedDept, getDeptOt(), accessableDeptList());
		}
		
		private function getDeptOt():Number
		{
			var ot:Number = 0;
			var row:int;
			for(var i:int=0; i<view.otList.length; i++) {
				if(view.otList.getItemAt(i).ORGEH == view.selectedDept) {
					var otField:String = "DAY";
					otField += (view.selectedDate.date<10) ? "0"+view.selectedDate.date:view.selectedDate.date;
					ot = parseInt(view.otList.getItemAt(i)[otField]);
					break;
				}
			}
			if(isNaN(ot)) ot = 0;
			return ot;
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():Overview
        {
            return viewComponent as Overview;
        }
	}
}